import {fgui} from "../../../../../../libs/fairygui.1.3.0";
import {designHeight, designWidth} from "../../../../../app";

export default class BaseFguiVc extends fgui.GComponent {

    constructor() {
        super();
        this.on(fgui.InteractiveEvents.Down, this.onDown);
    }

    onDown(e) {
        e.stopPropagation();
    }

    onLoaded() {
        // todo
    }

    getCompByName(name) {
        return this._ui ? this._ui.getChild(name) : null;
    }

    initUi(pkgName, resName, addBg = false) {
        if (fgui.UIPackage.getByName(pkgName)) {
            this._ui = fgui.UIPackage.createObject(pkgName, resName);
            if (this._ui) {
                if (addBg) {
                    this.background = this.generateGraph(designWidth, designHeight);
                    this.addChild(this.background);
                }

                this._ui.x = -designWidth / 2;
                this._ui.y = -designHeight / 2;
                this.addChild(this._ui);
            }
        }
    }

    show() {
        this.visible = true;
    }

    hide() {
        this.visible = false;
    }

    dispose() {
        if (this.background) {
            if (this.background.parent) {
                this.background.parent.removeChild(this.background);
            }
            this.background.dispose();
            this.background = null;
        }
        this._ui.dispose();
        this._ui = null;
        super.dispose();
        this.off(fgui.InteractiveEvents.Down, this.onDown);
    }

    generateGraph(w, h, c = 0, a = 1) {
        let graph = new fgui.GGraph();
        graph.x = -w / 2;
        graph.y = -h / 2;
        graph.width = w;
        graph.height = h;
        graph.drawRect(1, c,  a,  c , a);
        return graph;
    }
}